﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using P5.OpenIdEx.Objects;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;

namespace P5.OpenIdEx
{
    public class OpenIdHelper
    {
        /// <summary>
        /// Get authenticated information from OpenID provider
        /// </summary>
        /// <param name="openIdRelyingParty"></param>
        /// <param name="accountInfo"></param>
        /// <returns></returns>
        public static bool GetOpenIdResponseInfo(out OpenIdRelyingParty openIdRelyingParty, out OpenIdAccountInfo accountInfo)
        {
            accountInfo = new OpenIdAccountInfo();
            openIdRelyingParty = new OpenIdRelyingParty();

            bool localResult = false;
            var response = openIdRelyingParty.GetResponse();

            if (response != null)
            {
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        // This is where you would look for any OpenID extension responses included
                        // in the authentication assertion.
                        ClaimsResponse claimsResponse = response.GetExtension<ClaimsResponse>();
                        if (claimsResponse != null)
                        {
                        }

                        FetchResponse fetchResponse = response.GetExtension<FetchResponse>();
                        bool parseResult = accountInfo.ParseInfo(fetchResponse);

                        // Store off the "friendly" username to display -- NOT for username lookup
                        accountInfo.FriendlyIdentifier = response.FriendlyIdentifierForDisplay;

                        // Use FormsAuthentication to tell ASP.NET that the user is now logged in,
                        // with the OpenID Claimed Identifier as their username.
                        accountInfo.ClaimedIdentifier = response.ClaimedIdentifier;

                        localResult = true;
                        break;

                    case AuthenticationStatus.Failed:
                        break;

                    case AuthenticationStatus.Canceled:
                        break;
                }
            }

            return localResult;
        }

        /// <summary>
        /// Redirect to provider page to let user signs in or query user information
        /// </summary>
        /// <param name="openIdRelyingParty"></param>
        /// <param name="suppliedIdentifier"></param>
        public static void RedirectToProvider(OpenIdRelyingParty openIdRelyingParty, string suppliedIdentifier)
        {
            IAuthenticationRequest request = openIdRelyingParty.CreateRequest(suppliedIdentifier);

            // This is where you would add any OpenID extensions you wanted
            // to include in the authentication request.
            var fetch = OpenIdProviders.GetFetchRequest(false);
            request.AddExtension(fetch);

            //request.AddExtension(new ClaimsRequest
            //{
            //    Nickname = DemandLevel.Require,
            //    Email = DemandLevel.Require,
            //    FullName = DemandLevel.Require,
            //    BirthDate = DemandLevel.Require,
            //    Gender = DemandLevel.Require,
            //    Country = DemandLevel.Request,
            //    PostalCode = DemandLevel.Request,
            //    TimeZone = DemandLevel.Request,
            //});
            request.RedirectToProvider();
        }
    }
}
